const nian = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
let nianNumber = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
const yue = ['1号', '2号', '3号', '4号', '5号', '6号', '7号', '8号', '9号', '10号', '11号', '12号', '13号', '14号', '15号', '16号', '17号',
    '18号', '19号', '20号', '21号', '22号', '23号', '24号', '25号', '26号', '27号', '28号', '29号', '30号', '31号'];
let yueNumber1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let yueNumber2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
/*柱状图加载...*/
function website_bar_chart(date, number) {
    var data = {
        tooltip: {trigger: 'axis'},
        calculable: true,
        xAxis: [{
            type: 'category',
            data: date,
            position: 'left'
        }],
        yAxis: [{
            type: 'value',
            position: 'left'
        }],
        series: [{
            name: 'number',
            type: 'bar',
            data: number,
            color: '#009999' // 设置颜色
        }]
    }
    require.config({paths: {echarts: 'https://echarts.baidu.com/build/dist'}});
    // 使用
    // 使用柱状图就加载bar模块，按需加载

    require(['echarts', 'echarts/chart/bar', 'echarts/chart/line'],
        function (ec) {
            // 为echarts对象加载数据
            ec.init(document.getElementById('main2')).setOption(data);
        }
    );
}

/*查询当日的新增用户*/
function newUserNumber() {
    axios({
        method: "get",
        url: "/website-statistics/newUsers"
    }).then(res => {
        $("#new_user_number").html(res.data);
    });

    average_daily_number_of_new_users();
}

/*查询平均每日的活跃用户*/
function averageDailyActiveUsers() {
    axios({
        method: "get",
        url: "/website-statistics/averageDailyActiveUsers"
    }).then(res => {
        let temp = 0;
        const data = res.data;
        for (let i = 0; i < data.length; i++) {
            temp = temp + data[i].number;
        }
        $("#average_daily_active_users").html(temp / data.length);
    })

    newUserNumber();
}

/*查询当日的活跃用户*/
function dailyActiveUsers() {
    axios({
        method: "get",
        url: "/website-statistics/dailyActiveUsers"
    }).then(res => {
        $("#daily_active_users").html(res.data);
    });
    averageDailyActiveUsers();
}

/*查询某段时间的活跃用户
  * 2023-01-01/2023-12-31
  * */
function daily_active_users(kDate, jDate) {
    axios({
        method: "get",
        url: "/website-statistics/queryTheActiveUsersSpecifiedPeriodOfTime/" + kDate + "/" + jDate
    }).then(res => {
        yueNumber1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        let data = res.data;
        for (let i = 0; i < data.length; i++) {
            for (let j = 0; j < yue.length; j++) {
                if ((j + 1) == data[i].date) {
                    yueNumber1[j] = data[i].number;
                }
            }
        }
    });
    website_bar_chart(yue, yueNumber1)
}

/*日均新增用户量*/
function average_daily_number_of_new_users() {
    axios({
        method: "get",
        url: "/website-statistics/averageNewUserPerDay"
    }).then(res => {
        let temp = 0;
        const data = res.data;
        for (let i = 0; i < data.length; i++) {
            temp = temp + data[i].number;
        }
        $("#average_daily_number_of_new_users").html(temp / data.length);
    });
    daily_active_users("2023-06-01", "2023-06-30");
}

/*查询某段时间的活跃用户
  * 2023-01-01/2023-12-31
  * */
function userAddedWithinSpecifiedPeriod(kDate, jDate) {

    axios({
        method: "get",
        url: "/website-statistics/userAddedWithinSpecifiedPeriod/" + kDate + "/" + jDate
    }).then(res => {
        yueNumber2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        let data = res.data;
        for (let i = 0; i < data.length; i++) {
            for (let j = 0; j <= yue.length; j++) {
                if ((j + 1) == data[i].date) {
                    yueNumber2[j] = data[i].number;
                }
            }
        }
    });
    website_bar_chart(yue, yueNumber2)
}